﻿<!--_meta 作为公共模版分离出去-->
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<!--[if lt IE 9]>
<script type="text/javascript" src="__STATIC__/template/lib/html5shiv.js"></script>
<script type="text/javascript" src="__STATIC__/template/lib/respond.min.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href="__STATIC__/template/static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="__STATIC__/template/static/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="__STATIC__/template/lib/Hui-iconfont/1.0.8/iconfont.css" />
<link rel="stylesheet" type="text/css" href="__STATIC__/template/static/h-ui.admin/skin/default/skin.css" id="skin" />
<link rel="stylesheet" type="text/css" href="__STATIC__/template/static/h-ui.admin/css/style.css" />
<!--[if IE 6]>
<script type="text/javascript" src="__STATIC__/template/lib/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<!--/meta 作为公共模版分离出去-->

<title>角色添加</title>
</head>
<body>
<article class="page-container">
	<form action="" method="post" class="form form-horizontal" id="form-admin-role-add">
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色名称：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="" id="name" name="name">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">权限解释：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="" placeholder="" id="explain" name="explain">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">网站角色：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<!--<dl class="permission-list">
					<dt>
						<label>
							<input type="checkbox" value="1" name="super" id="super">
							超级管理员</label>
					</dt>
					<dd>
						<dl class="cl permission-list2">
							<dt>
								<label class="">
									<input type="checkbox" value="0" name="user-Character-1-0" id="user-Character-1-0">
									拥有一切权限
								</label>
							</dt>

						</dl>
					</dd>
				</dl>-->
				{volist name="result" id="v" key="k"}
				<dl class="permission-list">
					<dt>
						<label class="title">
							<input type="checkbox" value="{$v[0]['item_id']}" name="item_ids" class="item_id" onclick="changeMenu(this)" >
						{$key}
						</label>{$v[0]['item_id']}
					</dt>
					<dd>
						<dl class="cl permission-list2">
							{foreach name="$v" item="value"}
							<dt>
								<label class="menu">
									<input type="checkbox" value="{$value.id}" data-item-id="{$value.item_id}" name="menu_id[]" class="menu_id" >
									<span>{$value.name}</span>
								</label>
							</dt>
							{/foreach}
						</dl>
					</dd>

				</dl>
				{/volist}
			</div>
		</div>
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
				<button type="button" class="btn btn-success radius" id="role-save" name="admin-role-save">
					<i class="icon-ok"></i> 确定
				</button>
			</div>
		</div>
	</form>
</article>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="__STATIC__/template/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="__STATIC__/template/lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="__STATIC__/template/static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="__STATIC__/template/static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="__STATIC__/template/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="__STATIC__/template/lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="__STATIC__/template/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript">
	$('document').ready(function(){
		$('#role-save').click(roleSave);

		// 选中一个 菜单 取消一个菜单   start
		$(".menu_id").click(function () {
			var item =   $(this).attr('data-item-id');
			if ($(this).is(":checked")) {
				$(this).prop("checked", true);
				$(".item_id:checkbox[value="+item+"]").prop('checked', 'true');
				//alert("选中");
			} else {
				$(this).prop("checked", false);
				var l =  $("input[data-item-id="+item+"]:checked").length;  //获取菜单选中长度
				//console.log(l);
				if(l==0){
					$(".item_id:checkbox[value="+item+"]").prop("checked", false);
				}
				//alert("取消");
			}
		});
		//选中一个 菜单 取消一个菜单   end
	})

	/*菜单 - 选择*/
	function changeMenu(obj){
		//alert(1);
		if($(obj).attr('checked','checked')){
			//alert(1);
			$(obj).closest("dl").find("dd input:checkbox").prop("checked",$(obj).prop("checked"));
		}
	}
	function IsInArray(arr,val){
		//IE6不支持
		var testStr=','+arr.join(",")+",";

		return testStr.indexOf(","+val+",")!=-1;

	}
	Array.prototype.in_array = function (element) {

		for (var i = 0; i < this.length; i++) {

			if (this[i] == element) {

				return true;

			}

		} return false;

	}
	/*角色添加*/
	function roleSave(){
		var name 	= $('#name').val();
		var explain = $('#explain').val();
		var menu_id = new Array();
		var item_id   = new Array();

		var item_len = $("input:checkbox[class=item_id]:checked").length; //获取item长度
		console.log(item_len);
		var all = "";
		for(var i=0;i<item_len;i++){
			var xx = $("input:checkbox[class=item_id]:checked").eq(i);
			//console.log(xx);
			var item_ids = xx.val();
				var menu_len = xx.parents('dt').next('dd').find("input:checkbox[class=menu_id]:checked").length; //获取menu的长度
				var menu = xx.parents('dt').next('dd').find("input:checkbox[class=menu_id]:checked");
				var menu_ids = '';
				   for(var k=0;k<menu_len;k++){
						menu_ids += menu.eq(k).val()+",";
				   }
			all += item_ids+':'+menu_ids+';';

		}
		console.log(all);
		var url		= "{:url('admin/Role/roleSave')}";
		var data	= { name:name,explain:explain,all:all };
		$.post(url,data,function(res){
			console.log(res);

			if(res.ok){
				window.parent.location.reload();//刷新父页面
				//var index = parent.layer.getFrameIndex(window.name);
				parent.layer.close();//关闭当前页面
			}else{
				layer.msg(res.msg,{icon:5,time:1000})
			}

		})
	}
</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>
